<?php
echo html::printPath('userSpace.seminarChoice-title', 'userSpace-seminarChoice-desc');
$msg->printMessages();

$seminarCount = count($seminarList);
$viCount = count($viSeminarList);
if ($seminarCount + $viCount > 0) {
    $viIndex = 0;
    $seminarIndex = 0;
    $lastSeminarMonth = null;

    echo '<ul class="seminarChoiceList">';
    
    for ( ; $viIndex + $seminarIndex < $seminarCount + $viCount; ) {
        
        if ($seminarIndex == $seminarCount) {
            writeMonthCaption($viSeminarList[$viIndex], $lastSeminarMonth);
            writeViSeminar($viSeminarList[$viIndex]);
            $viIndex++;
        }
        else if ($viIndex == $viCount) {
            writeMonthCaption($seminarList[$seminarIndex], $lastSeminarMonth);
            writeSeminar($seminarList[$seminarIndex]);
            $seminarIndex++;
        }
        else {
            if (date::diffDates($seminarList[$seminarIndex]['begin_date'], $viSeminarList[$viIndex]['begin_date']) > 0) {
                writeMonthCaption($seminarList[$seminarIndex], $lastSeminarMonth);
                writeSeminar($seminarList[$seminarIndex]);
                $seminarIndex++;
            }
            else {
                writeMonthCaption($viSeminarList[$viIndex], $lastSeminarMonth);
                writeViSeminar($viSeminarList[$viIndex]);
                $viIndex++;
            }
        }
        echo '</li>';
    }
    echo '</ul>';
}
else {
    echo '<p>' . text::getEscaped('userSpace.welcome-seminars-noSeminar') . '</p>';
}

function writeMonthCaption($seminar, &$lastSeminarMonth) {
    $seminarMonth = date::month($seminar['begin_date']);
    if ($lastSeminarMonth != $seminarMonth) {
        echo '</ul>';
        echo '<h3>' . text::getEscaped('calendar-month-' . $seminarMonth) . ' ' . date::year($seminar['begin_date']) . '</h3>';
        echo '<ul class="seminarChoiceList">';
        $lastSeminarMonth = $seminarMonth;
    }
}

function writeSeminar($seminar) {
    echo '<li class="international">';
    echo html::anchor('/user/registrationWizard/enter?id=' . $seminar['id'], html::specialChars($seminar['name'])) .
        '<br/><span class="description" title="' . format::dateAndTime($seminar['begin_date'], $seminar['begin_time']) . ' &ndash; ' . format::dateAndTime($seminar['end_date'], $seminar['end_time']) . '">' . format::date($seminar['begin_date']) . ' &ndash; ' . format::date($seminar['end_date']) . '</span>';
    if ($seminar['description']) {
        echo '<p class="description">' . $seminar['description'] . '</p>';
    }
    if ($seminar['registrationList']) {
        echo '<p>' . text::getEscaped('registrationWizard.youAlreadyHaveRegistration') . '</p>';
        echo '<ul class="description">';
        foreach ($seminar['registrationList'] as $registration) {
            echo '<li>' . html::anchor('/user/myRegistration/display?type=reg&id=' . $registration['id'], text::get('registrationWizard.displayRegistration', format::timestamp($registration['created_at']))) . '</li>';
        }
        echo '</ul>';
    }
}

function writeViSeminar($viSeminar) {
    echo '<li>';
    if ($viSeminar['reg_id']) {
        echo html::specialchars($viSeminar['name']);
    }
    else {
        echo html::anchor('/user/viregWizard/enter?id=' . $viSeminar['id'], html::specialchars($viSeminar['name']));
    }
    echo '<br/><span class="description" title="' . format::dateAndTime($viSeminar['begin_date'], $viSeminar['begin_time']) . ' &ndash; ' . format::dateAndTime($viSeminar['end_date'], $viSeminar['end_time']) . '">' . format::date($viSeminar['begin_date']) . ' &ndash; ' . format::date($viSeminar['end_date']) . '</span>';
    if ($viSeminar['description']) {
        echo '<p class="description">' . $viSeminar['description'] . '</p>';
    }
    if ($viSeminar['reg_id']) {
        echo '<p>' . text::getEscaped('viregWizard.youAlreadyHaveRegistration', format::timestamp($viSeminar['reg_created_at'])) . '</p>';
        echo '<ul><li>' . html::anchor('/user/myRegistration/display?vi=' . $viSeminar['reg_id'], text::get('viregWizard.displayRegistration')) . '</li></ul>';
    }
}
